import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 * 由于分为各个子系统菜单，修改为constantRoutes 全改为hidden（首页也改为hidden）,
 */
export const constantRoutes = [{
  path: '/redirect',
  component: Layout,
  hidden: true,
  children: [{
    path: '/redirect/:path*',
    component: () => import('@/views/redirect/index')
  }]
},
{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},
{
  path: '/auth-redirect',
  component: () => import('@/views/login/auth-redirect'),
  hidden: true
},
{
  path: '/404',
  component: () => import('@/views/error-page/404'),
  hidden: true
},
{
  path: '/401',
  component: () => import('@/views/error-page/401'),
  hidden: true
},
{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  hidden: true,
  children: [{
    path: 'dashboard',
    component: () => import('@/views/dashboard/index'),
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'dashboard',
      affix: true
    }
  }]
},
{
  path: '/profile',
  component: Layout,
  redirect: '/profile/index',
  hidden: true,
  children: [{
    path: 'index',
    component: () => import('@/views/profile/index'),
    name: 'Profile',
    meta: {
      title: '个人中心',
      icon: 'user',
      noCache: true
    }
  }]
}
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [{
  path: '/sys',
  component: Layout,
  name: 'Sys',
  redirect: 'noRedirect',
  meta: {
    title: '系统管理',
    icon: 'el-icon-setting'
  },
  children: [{
    path: 'userlist',
    component: () => import('@/views/sys/user/index'),
    name: 'UserList',
    meta: {
      title: '用户管理',
      icon: 'user',
      noCache: false
    }
  },
  {
    path: 'userAdd',
    component: () => import('@/views/sys/user/form'),
    name: 'UserAdd',
    hidden: true,
    meta: {
      title: '用户新增',
      icon: 'user',
      noCache: true,
      activeMenu: '/sys/user'
    }
  },
  {
    path: 'userEdit/:id',
    component: () => import('@/views/sys/user/form'),
    name: 'UserEdit',
    hidden: true,
    meta: {
      title: '用户编辑',
      icon: 'user',
      noCache: true,
      activeMenu: '/sys/user'
    }
  },
  {
    path: 'role',
    component: () => import('@/views/sys/role/index'),
    name: 'Role',
    meta: {
      title: '角色管理',
      icon: 'peoples',
      noCache: true
    }
  },
  {
    path: 'group',
    component: () => import('@/views/sys/group/index'),
    name: 'Group',
    meta: {
      title: '群组管理',
      icon: 'group',
      noCache: true
    }
  },
  {
    path: 'position',
    component: () => import('@/views/sys/position/index'),
    name: 'Position',
    meta: {
      title: '岗位管理',
      icon: 'supplier',
      noCache: true
    }
  },
  {
    path: 'permission',
    component: () => import('@/views/sys/permission/index'),
    name: 'Permission',
    meta: {
      title: '权限管理',
      icon: 'menu',
      noCache: true
    }
  },
  {
    path: 'department',
    component: () => import('@/views/sys/department/index'),
    name: 'Department',
    meta: {
      title: '组织架构',
      icon: 'tree',
      noCache: true
    }
  },
  {
    path: 'dirtionary',
    component: () => import('@/views/sys/dirtionary/index'),
    name: 'Dirtionary',
    meta: {
      title: '字典管理',
      icon: 'list',
      noCache: true
    }
  },
  {
    path: 'process',
    component: () => import('@/views/sys/process/index'),
    name: 'Process',
    meta: {
      title: '流程管理',
      icon: 'example',
      noCache: true
    }
  },
  {
    path: 'processdesign',
    component: () => import('@/views/sys/process/designer'),
    name: 'ProcessDesign',
    hidden: true,
    meta: {
      title: '流程设计',
      icon: 'loan',
      noCache: true,
      activeMenu: '/sys/process'
    }
  },
  {
    path: 'sysparams',
    component: () => import('@/views/sys/sysparams/index'),
    name: 'SysParams',
    meta: {
      title: '系统参数',
      icon: 'setting',
      noCache: true
    }
  },
  {
    path: 'impresult',
    component: () => import('@/views/sys/impresult/index'),
    name: 'ImpResult',
    meta: {
      title: '导入结果',
      icon: 'bug',
      noCache: true
    }
  }
  ]
},
{ path: '/edu',
  component: Layout,
  name: 'Edu',
  redirect: 'noRedirect',
  meta: {
    title: '教务管理',
    icon: 'el-icon-date'
  },
  children: [{
    path: 'processinst',
    component: () => import('@/views/act/processinst/index'),
    name: 'ProcessInst',
    meta: {
      title: '我的申请',
      icon: 'out',
      noCache: true
    }
  },
  {
    path: 'task',
    component: () => import('@/views/act/task/index'),
    name: 'Task',
    meta: {
      title: '我的任务',
      icon: 'check',
      noCache: true
    }
  },
  {
    path: 'socialjob',
    redirect: 'noRedirect',
    component: () => import('@/views/edu/socialjob/index'),
    name: 'SocialJob',
    meta: {
      title: '社会任职',
      icon: 'link'
    },
    children: [
      {
        path: 'apply',
        component: () => import('@/views/edu/socialjob/apply/index'),
        name: 'SocialJobApply',
        meta: {
          title: '社会任职申请',
          icon: 'form',
          noCache: true
        }
      },

      {
        path: 'list',
        component: () => import('@/views/edu/socialjob/list/index'),
        name: 'SocialJobList',
        meta: {
          title: '社会任职管理',
          icon: 'list',
          noCache: true
        }
      }
    ]
  },
  {
    path: 'publish',
    redirect: 'noRedirect',
    component: () => import('@/views/edu/publish/index'),
    name: 'Publish',
    meta: {
      title: '教师发表',
      icon: 'document'
    },
    children: [
      {
        path: 'apply',
        component: () => import('@/views/edu/publish/apply/index'),
        name: 'PublishApply',
        meta: {
          title: '教师发表申请',
          icon: 'form',
          noCache: true
        }
      },
      {
        path: 'list',
        component: () => import('@/views/edu/publish/list/index'),
        name: 'PublishList',
        meta: {
          title: '教师发表管理',
          icon: 'list',
          noCache: true
        }
      }
    ]
  },
  {
    path: 'award',
    redirect: 'noRedirect',
    component: () => import('@/views/edu/award/index'),
    name: 'Award',
    meta: {
      title: '荣誉',
      icon: 'supplier'
    },
    children: [
      {
        path: 'apply',
        component: () => import('@/views/edu/award/apply/index'),
        name: 'AwardApply',
        meta: {
          title: '荣誉申请',
          icon: 'form',
          noCache: true
        }
      },
      {
        path: 'list',
        component: () => import('@/views/edu/award/list/index'),
        name: 'AwardList',
        meta: {
          title: '荣誉管理',
          icon: 'list',
          noCache: true
        }
      }
    ]
  },
  {
    path: 'training',
    redirect: 'noRedirect',
    component: () => import('@/views/edu/training/index'),
    name: 'Training',
    meta: {
      title: '学习交流',
      icon: 'edit'
    },
    children: [
      {
        path: 'apply',
        component: () => import('@/views/edu/training/apply/index'),
        name: 'TrainingApply',
        meta: {
          title: '学习交流申请',
          icon: 'form',
          noCache: true
        }
      },
      {
        path: 'list',
        component: () => import('@/views/edu/training/list/index'),
        name: 'TrainingList',
        meta: {
          title: '学习交流管理',
          icon: 'list',
          noCache: true
        }
      }
    ]
  },
  {
    path: 'leave',
    redirect: 'noRedirect',
    component: () => import('@/views/edu/leave/index'),
    name: 'Leave',
    meta: {
      title: '员工请假',
      icon: 'timer'
    },
    children: [
      {
        path: 'apply',
        component: () => import('@/views/edu/leave/apply/index'),
        name: 'LeaveApply',
        meta: {
          title: '员工请假申请',
          icon: 'form',
          noCache: true
        }
      },
      {
        path: 'list',
        component: () => import('@/views/edu/leave/list/index'),
        name: 'LeaveList',
        meta: {
          title: '员工请假管理',
          icon: 'list',
          noCache: true
        }
      }
    ]
  }
  ]
}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
